<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<script type="text/javascript" src="<%=request.getContextPath()%>/resources/js/common/jquery-2.1.0.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/resources/js/common/jquery.form.js"></script>
<%--<script type="text/javascript" src="<%=request.getContextPath()%>/resources/plugins/layui/layui.js"></script>--%>
<script type="text/javascript" src="<%=request.getContextPath()%>/resources/plugins/layui/layui.all.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/resources/js/angular.min.js"></script>
<%--<script type="text/javascript" src="<%=request.getContextPath()%>/resources/dataTable/js/jquery.dataTables.min.js"></script>--%>
<script type="text/javascript" src="<%=request.getContextPath()%>/resources/datatables/media/js/jquery.dataTables.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/resources/datatables/plugins/bootstrap/dataTables.bootstrap.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/resources/js/config.js"></script>

<script>
    $(function () {
        $(".fa-refresh").click(function () {
            location.reload();
        });
    });

    //遍历验证权限
    function checkJurisdiction () {
        var menuList = AM.getLoginUserJurisdiction();
        for (var i = 0; i < menuList.length; i++) {
            var oneChild = menuList[i];
            checkJurisdiction2(oneChild)
        }

    }

    function checkJurisdiction2(menuList) {
        for (var j = 0; j < menuList.child.length; j++) {
            var twoChild = menuList.child[j];
            checkJurisdiction2(twoChild)
            $(".checkBtn_" + twoChild.id).show();
        }
    }

    function buildBusinessInput(selectId,targetView,method,isShow){
        AM.ajaxRequestData(method, false, AM.ip + "/business/queryBusiness", {} , function(result){
            if(result.flag == 0 && result.code == 200){
                var html = "";
                for (var i = 0; i < result.data.length; i++) {
                    if(result.data[i].id == 1 || result.data[i].id == 2){
                        continue;
                    }
                    if (result.data[i].id == selectId) {
                        html += '<input type="checkbox" value="'+result.data[i].id+'" lay-filter = "boxChecked"  name="businessArr" title="'+result.data[i].businessName+'" checked>';
                    }
                    else {
                        html += '<input type="checkbox" value="'+result.data[i].id+'" lay-filter = "boxChecked" name="businessArr" title="'+result.data[i].businessName+'">';
                    }
                }
                $("#"+targetView+"").html(html);
                if(1 == isShow){
                    $("#"+targetView).parent().parent().show();
                }

            }
        });
    }







    //省
    function selectProvince(selectId,targetView) {
        AM.ajaxRequestData("POST", false, AM.ip + "/city/queryCityByParentId", {levelType : 1} , function(result){
            var html = "<option value=''>请选择或搜索省</option>";
            for (var i = 0; i < result.data.length; i++) {
                if (result.data[i].id == selectId) {
                    html += "<option selected=\"selected\" title=\"" + result.data[i].mergerName + "\" value=\"" + result.data[i].id + "\">" + result.data[i].shortName + "</option>";
                }
                else {
                    html += "<option title=\"" + result.data[i].mergerName + "\" value=\"" + result.data[i].id + "\">" + result.data[i].shortName + "</option>";
                }
            }
            if (result.data.length == 0) {
                html += "<option value=\"0\" disabled>暂无</option>";
            }
            $("#"+targetView+"").html(html)
            // $("select[name='province']").html(html);
        });
    }

    //市
    function selectCity(cityId, selectId,targetView) {
        AM.ajaxRequestData("POST", false, AM.ip + "/city/queryCityByParentId", {cityId : cityId, levelType : 2} , function(result){
            var html = "<option value=\"\">请选择或搜索市</option>";
            for (var i = 0; i < result.data.length; i++) {
                if (result.data[i].id == selectId) {
                    html += "<option selected=\"selected\" title=\"" + result.data[i].mergerName + "\" value=\"" + result.data[i].id + "\">" + result.data[i].shortName + "</option>";
                }
                else {
                    html += "<option title=\"" + result.data[i].mergerName + "\" value=\"" + result.data[i].id + "\">" + result.data[i].shortName + "</option>";
                }
            }
            if (result.data.length == 0) {
                html += "<option value=\"0\" disabled>暂无</option>";
            }
            $("#"+targetView+"").html(html);
            // $("select[name='city']").html(html);
        });
    }

    //区
    function selectCounty(cityId, selectId,targetView) {
        AM.ajaxRequestData("POST", false, AM.ip + "/city/queryCityByParentId", {cityId : cityId, levelType : 3} , function(result){
            var html = "<option value=\"\">请选择或搜索县/区</option>";
            for (var i = 0; i < result.data.length; i++) {
                if (result.data[i].id == selectId) {
                    html += "<option selected=\"selected\" title=\"" + result.data[i].mergerName + "\" value=\"" + result.data[i].id + "\">" + result.data[i].shortName + "</option>";
                }
                else {
                    html += "<option title=\"" + result.data[i].mergerName + "\" value=\"" + result.data[i].id + "\">" + result.data[i].shortName + "</option>";
                }
            }
            if (result.data.length == 0) {
                html += "<option value=\"0\" disabled>暂无</option>";
            }
            $("#"+targetView+"").html(html);
            // $("select[name='district']").html(html);
        });
    }

    /**省市分组**/
    function selectProvinceAndCity(selectId, targetView) {
        AM.ajaxRequestData("POST", false, AM.ip + "/city/getCities", {} , function(result){
            var optgroup = "<option value=\"\">请选择或搜索</option>";
            for (var i = 0; i < result.data.length; i++) {
                var obj = result.data[i];
                var option = "";
                for (var j = 0; j < obj.cityList.length; j++) {
                    var city = obj.cityList[j];
                    if (city.id == selectId) {
                        option += '<option selected="selected"  value="' + city.id + '">' + city.name + '</option>';
                    }
                    else {
                        option += '<option value="' + city.id + '">' + city.name + '</option>';
                    }
                }
                //optgroup += "<optgroup label=\"" + obj.name + "\">" + option + "</optgroup>";
                optgroup += option;
            }
            if (result.data.length == 0) {
                optgroup += "<option value=\"0\" disabled>暂无</option>";
            }
            $("select[name="+targetView+"]").html(optgroup);
        });
    }



    //获取角色列表
    function getUserByRole (selectId,targetView,isShow,roleId) {

        AM.ajaxRequestData("get", false, AM.ip + "/user/queryUserByRole", {roleId:roleId} , function(result){
            if(result.flag == 0 && result.code == 200){
                var html = "<option value=\"\">请选择角色类型</option>";
                for (var i = 0; i < result.data.length; i++) {
                    if (result.data[i].id == selectId) {
                        html += "<option selected=\"selected\" value=\"" + result.data[i].id + "\">" + result.data[i].userName + "</option>";
                    }
                    else {
                        html += "<option value=\"" + result.data[i].id + "\">" + result.data[i].userName + "</option>";
                    }
                }
                if (result.data.length == 0) {
                    html += "<option value=\"0\" disabled>暂无</option>";
                }
                $("select[name='"+targetView+"']").html(html);
                if(1 == isShow){
                    $("select[name='"+targetView+"']").parent().parent().show();
                }
            }
        });
    }


    //获取所有的公司
    function buildAllCompanySelect (selectId,targetView) {
        AM.ajaxRequestData("post", false, AM.ip + "/company/allCompany", {} , function(result){
            if(result.flag == 0 && result.code == 200){
                companyList = result.data;
                var html = "<option value=\"\">请选择</option>";
                for (var i = 0; i < result.data.length; i++) {
                    if (result.data[i].id == selectId) {
                        html += "<option selected=\"selected\" value=\"" + result.data[i].id + "\">" + result.data[i].companyName + "</option>";
                    }
                    else {
                        html += "<option value=\"" + result.data[i].id + "\">" + result.data[i].companyName + "</option>";
                    }
                }
                if (result.data.length == 0) {
                    html += "<option value=\"0\" disabled>暂无</option>";
                }
                $("#"+targetView+"").html(html);
            }
        });
    }



    //获取一类列表
    function buildCategorySelect (selectId,targetView) {
        AM.ajaxRequestData("post", false, AM.ip + "/qualityCategory/getAll", {} , function(result){
            if(result.flag == 0 && result.code == 200){
                companyList = result.data;
                var html = "<option value=\"\">请选择</option>";
                for (var i = 0; i < result.data.length; i++) {
                    if (result.data[i].id == selectId) {
                        html += "<option selected=\"selected\" value=\"" + result.data[i].id + "\">" + result.data[i].categoryName + "</option>";
                    }
                    else {
                        html += "<option value=\"" + result.data[i].id + "\">" + result.data[i].categoryName + "</option>";
                    }
                }
                if (result.data.length == 0) {
                    html += "<option value=\"0\" disabled>暂无</option>";
                }
                $("#"+targetView+"").html(html);
            }
        });
    }

    //获取一类列表
    function buildCategoryChildSelect (selectId,targetView,categoryId) {
        AM.ajaxRequestData("post", false, AM.ip + "/categoryChild/getChildList",
            {categoryId:categoryId} , function(result){
            if(result.flag == 0 && result.code == 200){
                companyList = result.data;
                var html = "<option value=\"\">请选择</option>";
                for (var i = 0; i < result.data.length; i++) {
                    if (result.data[i].id == selectId) {
                        html += "<option selected=\"selected\" value=\"" + result.data[i].id + "\">" + result.data[i].childName + "</option>";
                    }
                    else {
                        html += "<option value=\"" + result.data[i].id + "\">" + result.data[i].childName + "</option>";
                    }
                }
                if (result.data.length == 0) {
                    html += "<option value=\"0\" disabled>暂无</option>";
                }
                $("#"+targetView+"").html(html);
            }
        });
    }

    //获取类列表
    function buildQualityItemSelect (selectId,targetView,childId) {
        AM.ajaxRequestData("post", false, AM.ip + "/qualityItem/getQualityItemList",
            {childId:childId} , function(result){
            if(result.flag == 0 && result.code == 200){
                companyList = result.data;
                var html = "<option value=\"\">请选择</option>";
                for (var i = 0; i < result.data.length; i++) {
                    if (result.data[i].id == selectId) {
                        html += "<option attachment='"+result.data[i].attachment+"' selected=\"selected\" value=\"" + result.data[i].id + "\">" + result.data[i].qualityName + "</option>";
                    }
                    else {
                        html += "<option attachment='"+result.data[i].attachment+"' value=\"" + result.data[i].id + "\">" + result.data[i].qualityName + "</option>";
                    }
                }
                if (result.data.length == 0) {
                    html += "<option value=\"0\" disabled>暂无</option>";
                }
                $("#"+targetView+"").html(html);
            }
        });
    }

    //获取所有的公司
    function buildDepartmentSelect (companyId,selectId,targetView) {
        AM.ajaxRequestData("post", false, AM.ip + "/department/getDepartmentByCompany", {companyId:companyId} , function(result){
            if(result.flag == 0 && result.code == 200){
                companyList = result.data;
                var html = "<option value=\"\">请选择</option>";
                for (var i = 0; i < result.data.length; i++) {
                    if (result.data[i].id == selectId) {
                        html += "<option selected=\"selected\" value=\"" + result.data[i].id + "\">" + result.data[i].departmentName + "</option>";
                    }
                    else {
                        html += "<option value=\"" + result.data[i].id + "\">" + result.data[i].departmentName + "</option>";
                    }
                }
                if (result.data.length == 0) {
                    html += "<option value=\"0\" disabled>暂无</option>";
                }
                $("#"+targetView+"").html(html);
            }
        });
    }



    //获取角色列表
    function getRoleList (selectId,targetViewId) {
        AM.ajaxRequestData("get", false, AM.ip + "/role/list", {} , function(result){
            if(result.flag == 0 && result.code == 200){
                var html = "<option value=\"\">请选择角色类型</option>";
                for (var i = 0; i < result.data.length; i++) {
                    if (result.data[i].id == selectId) {
                        html += "<option selected=\"selected\" value=\"" + result.data[i].id + "\">" + result.data[i].roleName + "</option>";
                    }
                    else {
                        html += "<option value=\"" + result.data[i].id + "\">" + result.data[i].roleName + "</option>";
                    }
                }
                if (result.data.length == 0) {
                    html += "<option value=\"0\" disabled>暂无</option>";
                }
                $("#"+targetViewId+"").html(html);
            }
        });
    }

    //获取角色列表
    function buildRoleCheck (targetViewId) {
        AM.ajaxRequestData("post", false, AM.ip + "/role/list", {} , function(result){
            if(result.flag == 0 && result.code == 200){
                var html = "";
                for (var i = 0; i < result.data.length; i++) {
                    html += "<input type='checkbox' value='"+result.data[i].id+"' name='roles' title='"+result.data[i].roleName+"'>"
                }
                $("#"+targetViewId+"").html(html);
            }
        });
    }





    $(function () {
        checkJurisdiction();

        document.onkeydown = function(event) {
            var code;
            if (!event) {
                event = window.event; //针对ie浏览器
                code = event.keyCode;
                if (code == 13) {
                    if (document.getElementsByClassName("layui-layer-btn0").length > 0) {
                        document.getElementsByClassName("layui-layer-btn0")[0].click();
                    }
                    if (document.getElementById("unlock")) {
                        document.getElementById("unlock").click();
                    }
                }
            }
            else {
                code = event.keyCode;
                if (code == 13) {
                    if (document.getElementsByClassName("layui-layer-btn0").length > 0) {
                        document.getElementsByClassName("layui-layer-btn0")[0].click();
                    }
                    if (document.getElementById("unlock")) {
                        document.getElementById("unlock").click();
                    }
                }
            }
        };

        $(".layui-btn").blur();

    });



</script>